<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*" import="wildcard.database.DBAccess"
	import="wildcard.servlet.LoginBean"%>
<%@ page language="java" session="true"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>メインメニュー</title>
</head>
<body>


	<%
		LoginBean loginBean = null;
		String dbId = null;
		String dbPassword = null;

		// ユーザIDの取得
		if (session.getAttribute("LoginUser") != null) {
			loginBean = (LoginBean) session.getAttribute("LoginUser");
			dbId = loginBean.getLoginId().substring(0, 2);
			dbPassword = loginBean.getPassword();
		}

		// テーブル情報の取得
		ArrayList<String> parameters = new ArrayList<String>();
		parameters.add("SQL2");
		ArrayList<ArrayList<String>> resultList = DBAccess.executeSelect(
				"AN", "wildcard", DBAccess.SQL_SELECT_COMMAND, parameters);

		String sql = resultList.get(1).get(0);
		parameters.clear();

		resultList = DBAccess.executeSelect("AN", "wildcard", sql,
				parameters);
	%>

	<table border="1">
		<tr>
			<td bgcolor="#AAAAAA">
				<%
					Date date = new Date();
					Calendar cal = Calendar.getInstance();

					SimpleDateFormat time = new SimpleDateFormat(
							"yyyy年MM月dd日(E) HH:mm:ss");
				%> <%=time.format(date)%>
			</td>
			<td bordercolor="#FFFFFF">
				<form action=android_main.jsp>
					<button type="submit">リセット</button>
				</form>
			</td>
			<td bordercolor="#FFFFFF">
				<form action=android_main.jsp>
					<button type="submit">待機へ</button>
				</form>
			</td>
		</tr>
		<tr>
			<td bgcolor="#AAAAAA" Align="center">メイン</td>
		</tr>
		<tr>
			<td bgcolor="#AAAAAA" Align="center">テーブル情報</td>
		</tr>
		<tr>
			<td bordercolor="#FFFFFF" Align="center">
				<form action=orderrecord>
					<button type="submit">配膳状況へ</button>
				</form>
			</td>
			<td>
				<form action=orderrecord>
					<button type="submit">テーブル登録(人数)へ</button>
				</form>
			</td>
		</tr>
	</table>

	<br>


	<form action=tablerecord>
		<table border="1">
			<%
				for (int row = 0; row < resultList.size(); row++) {
			%>
			<tr>
				<%
					for (int column = -1; column < resultList.get(0).size(); column++) {
				%>

				<%
					if (column == -1) {
								if (row == 0) {
				%>
				<td></td>
				<%
					} else {
				%>
				<td><input type="radio" name="tableid"
					value="<%=resultList.get(row).get(0)%>"></td>
				<%
					}
							} else {
				%>
				<td><%=resultList.get(row).get(column)%></td>
				<%
					}
						}
				%>
			</tr>
			<%
				}
			%>
		</table>
	</form>
	<br>

	<%--メニュー情報 --%>

	<%
		// メニュー情報の取得
		parameters.clear();
		parameters.add("SQL8");
		resultList = DBAccess.executeSelect("AN", "wildcard",
				DBAccess.SQL_SELECT_COMMAND, parameters);

		sql = resultList.get(1).get(0);
		parameters.clear();
		System.out.println(parameters.size());
		resultList = DBAccess.executeSelect("AN", "wildcard", sql,
				parameters);
	%>

	<form action=orderrecord>
		<fieldset>
			<%
				ArrayList<String> labels = resultList.get(0);
				String categoryName = resultList.get(1).get(1);
				String beforeCategoryName = categoryName;

				for (int row = 1; row < resultList.size(); row++) {
			%>
			<table border="1">
				<tr>
					<th><%=categoryName%></th>
				</tr>

				<%
					while (beforeCategoryName.equals(categoryName)) {
				%>
				<tr>
					<%
						// 用いるデータをチェックし、必要な変数を用意する
								String id = null;
								String bgcolor = "#FFFFFF";

								for (int column = 0; column < resultList.get(row).size(); column++) {
									String data = resultList.get(row).get(column);
									String label = labels.get(column);

									switch (column) {
									case 0:
										id = data;
										break;
									case 4:

										if ("1".equals(resultList.get(row).get(column))) {
											bgcolor = "#777777";
										}

										break;
									default:
										break;

									}

								}
								for (int column = 0; column < resultList.get(row).size(); column++) {
									String data = resultList.get(row).get(column);
									String label = labels.get(column);
									switch (column) {
									case 0:
									case 1:
									case 4:
										break;
									default:
					%>
					<td bgcolor="<%=bgcolor%>"><%=data%></td>
					<%
						break;
									} // switchの終点
								} // カラム情報の終点
									// 数量入力を追加する
					%>

					<td><input type="number" name="<%=id%>" maxlength="1" min="0"
						max="9" value="0"></td>

					<%
						// 次の行へ移行する
								row++;
								if (row < resultList.size()) {
									categoryName = resultList.get(row).get(1);
								} else {
									break;
								}
							} // whileのカッコ
					%>
				</tr>
				<tr>
					<td colspan="3">
						<button type="submit">オーダー確認画面へ</button>
					</td>
				</tr>
				<%
					// テーブルタグを閉じる
						row--;
						beforeCategoryName = categoryName;
				%>

			</table>
			<br>
			<%
				}// ループ終点
			%>
		</fieldset>
	</form>
</body>
</html>